package com.cxkj.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.cxkj.dao.MeentryMapper;
import com.cxkj.dto.Meentry;
import com.cxkj.service.MeenttryService;
import com.cxkj.tools.Page;

@Service("meentryService")
public class MeentryServiceImpl implements MeenttryService{
	@Autowired
	private MeentryMapper meentryMapper;
	
	//商户录入搜索
	public void showMeByVolume(String mvolume, Model model) {
		
		List<Meentry> meList = meentryMapper.meenTest(mvolume);
		if(meList.size() != 0){
			model.addAttribute("meList", meList);
		}
		
	}
	//商户录入汇总/搜索
	public void showMeAll(String pageNow,String mvolume,String msogoname,Model model) {
		Page page = null;
		int count = (int)meentryMapper.getCount();
		List<Meentry> meList = new ArrayList<Meentry>();
		//录入汇总按照册号、用户名搜搜
		if((mvolume == null) || (msogoname == null) ){
			//商户录入汇总分页查询
			if(pageNow != null){
				page = new Page(count, Integer.valueOf(pageNow));
				meList = meentryMapper.showMeByCount(page.getStartPos(), page.getPageSize());
			}else{
				page = new Page(count, 1);
				meList = meentryMapper.showMeByCount(page.getStartPos(), page.getPageSize());
			}
			model.addAttribute("page", page);
			model.addAttribute("meList", meList);
		}else{
			
			meList = meentryMapper.souMeen(mvolume, msogoname);
			model.addAttribute("meList", meList);
		}
		
		
	}
	
	
	
	//进入结算信息页面
	@SuppressWarnings("unused")
	public void settlement(HttpServletRequest request, String pageNow, Model model) {
		List<Meentry> meList = null;
		Page page = null;
		String jieDate = request.getParameter("jieDate");
		if(jieDate != null){
			meList = meentryMapper.settlementByJiedate(jieDate);
			
		}else{
			int jieCount = (int)meentryMapper.getCountByJie();
			if(pageNow != null){
				page = new Page(jieCount, Integer.valueOf(pageNow));
				meList = meentryMapper.showJieByCount(page.getStartPos(), page.getPageSize());
			}else{
				page = new Page(jieCount, 1);
				meList = meentryMapper.showJieByCount(page.getStartPos(), page.getPageSize());
			}
			
		}
		//合计信息
		Meentry meSum = new Meentry();
		meSum.setMvolume("合计");
		//总重量
		meSum.setMweight("0");
		//总金额
		meSum.setMamount(new BigDecimal(0));
		//总手续费
		meSum.setMcommission(new BigDecimal(0));
		
		for (int i = 0; i < meList.size(); i++) {
			//总重量
			meSum.setMweight(String.valueOf(Integer.valueOf(meSum.getMweight())+Integer.valueOf(meList.get(i).getMweight())));
			//总金额
			meSum.setMamount(meSum.getMamount().add(meList.get(i).getMamount()));
			//总手续费
			meSum.setMcommission(meSum.getMcommission().add(meList.get(i).getMcommission()));
		}
		meList.add(meSum);
		
		model.addAttribute("page", page);
		model.addAttribute("meList", meList);
		
	}
	//农户录入信息验证金额
	@Override
	public List<Meentry> farmerTest(String djh, String je) {
		
		return meentryMapper.farmerTest(djh, je);
	}
	
	
	
	
	
	

}
